$(function(){



	var typeList = $('#type_list').find('li').find('a'),

		pages = parseInt($('#pages').val()),
		currentPage = parseInt($('#current_page').val()),
		pageController = $('#page_controller'),
		controllerList = $('#page_controller').find('ul'),
		leftPage = parseInt($('#current_page').val()) -2 ,
		rightPage = parseInt($('#current_page').val()) +2,
		html = ''



	var queryObject = getUrlObj(decodeURI(location.search.substring(1)))

	if(!queryObject.type){

		queryObject.type = 'all'
	}

	changeNavClass()


	if(currentPage == 1 ){

		html+='<li class="page-controller-item"><span><i class="fa fa-angle-double-left"></i></span></li>'+
			  '<li class="page-controller-item"><span><i class="fa fa-angle-left"></i></span></li>'
	}else{

		html+='<li class="page-controller-item"><a href="/index?type='+queryObject.type+'&p=1"><i class="fa fa-angle-double-left"></i></a></li>'+
			  '<li class="page-controller-item"><a href="/index?type='+queryObject.type+'&p='+(currentPage-1)+'"><i class="fa fa-angle-left"></i></a></li>'
	}


	if(leftPage >=2){
		html+='<li class="page-controller-item">...</li>'
	}


	for(var i=0; i<=5; i++){

		var nowPage = leftPage + i

		if(nowPage>=1 && nowPage<=pages){

			if(nowPage == currentPage){

				html+='<li class="page-controller-item active"><span>'+nowPage+'</span></li>'

			}else{

			html+='<li class="page-controller-item"><a href="/index?type='+queryObject.type+'&p='+nowPage+'">'+nowPage+'</a></li>'

			}

		}
	}

	if(pages - rightPage >=2){

		html+='<li class="page-controller-item">...</li>'
	}
	//防止当前页为1，而总页数为0时的bug
	if(currentPage >= pages){

		html+='<li class="page-controller-item"><span><i class="fa fa-angle-right"></i></span></li>'+
			  '<li class="page-controller-item"><span><i class="fa fa-angle-double-right"></i></span></li>'
			  
	}else{

		html+='<li class="page-controller-item"><a href="/index?type='+queryObject.type+'&p='+(currentPage+1)+'"><i class="fa fa-angle-right"></i></a></li>'+
			  '<li class="page-controller-item"><a href="/index?type='+queryObject.type+'&p='+pages+'"><i class="fa fa-angle-double-right"></i></a></li>'
	}

	controllerList.append(html)
	
  
function changeNavClass(){

		if(!queryObject.type || queryObject.type == 'all'){

		$('#type_list').find('li').eq(0).addClass("active")

		}

		else{

			typeList.each(function(key,item){

			if( $(this).attr('data-type') == queryObject.type ){

				$(this).parent().addClass("active")

				}
		
			})

		}
	}

	




	

	
	
	
})